<template>
  <div class="article-container">
    <el-card class="filter-card">
      <div slot="header" class="clearfix">
        <!-- 面包屑导航 -->
        <el-breadcrumb separator-class="el-icon-arrow-right">
          <el-breadcrumb-item to="/">首页</el-breadcrumb-item>
          <el-breadcrumb-item>内容管理</el-breadcrumb-item>
        </el-breadcrumb>
        <!-- /面包屑导航 -->
      </div>
      <!-- 数据筛选表单 -->
      <el-form
        ref="form"
        :model="form"
        label-width="40px"
        size="small"
      >
        <el-form-item label="状态">
          <el-radio-group v-model="status">
            <el-radio :label="null">全部</el-radio>
            <el-radio :label="0">草稿</el-radio>
            <el-radio :label="1">待审核</el-radio>
            <el-radio :label="2">审核通过</el-radio>
            <el-radio :label="3">审核失败</el-radio>
            <el-radio :label="4">已删除</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="频道">
          <el-select v-model="channelId" placeholder="请选择频道">
            <el-option label="全部" :value="null"></el-option>
            <el-option
              v-for="(item, index) in channels"
              :key="index"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="日期">
          <el-date-picker
            v-model="rangeDate"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            format="yyyy-MM-dd"
            value-format="yyyy-MM-dd"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            :disabled="loading"
            @click="loadArticles(1)"
          >筛选</el-button>
        </el-form-item>
      </el-form>
      <!-- /数据筛选表单 -->
    </el-card>
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>根据筛选条件共查询到{{ totalCount }}条结果：</span>
      </div>
      <!-- 数据列表 -->
      <!-- 1. 把需要展示的数组列表数据绑定给 table 组件的 data 属性
          注意： 不需要再去 v-for 遍历数据了，它内部会自己遍历处理
      2. 设计表格列 el-table-column
          label 可以设定列的标题
          prop 用来设定要渲染的列表项数据字段，只能展示文本
      3. 表格列默认只能渲染普通文本，如果需要展示其他内容，例如：按钮、图片、超链接等，那就需要自定义表格列模板了 -->
      <el-table
        class="list-table"
        :data="articles"
        style="width: 100%"
        stripe
        border
        size="small"
        v-loading="loading"
        element-loading-text="加载中..."
        element-loading-spinner="el-icon-loading"
        element-loading-background="rgba(0, 0, 0, 0.3)"
      >
        <el-table-column
          prop="date"
          label="封面"
          align="center">
          <template slot-scope="scope">
            <!-- <div v-if="scope.row.cover.images[0]" class="article-cover" >
              <img :src="scope.row.cover.images[0]" alt="">
            </div>
            <div v-else class="article-cover" >
              <img src="@/assets/image/pic_bg.png" alt="">
            </div> -->
            <div class="article-cover">
              <el-image
                :src="scope.row.cover.images[0]"
                fit="cover"
                lazy
              >
                  <div slot="placeholder" class="image-slot">
                    加载中<span class="dot">...</span>
                  </div>
                  <div slot="error" class="image-slot">
                    <img src="@/assets/image/pic_bg.png" alt="">
                  </div>
                </el-image>
              </div>
          </template>
        </el-table-column>
        <el-table-column
          prop="title"
          label="标题"
          align="center">
        </el-table-column>
        <el-table-column
          label="状态"
          align="center"
        >
          <!-- 如果需要在自定义模板中获取当前遍历项数据，那么就在 template 上声明 slot-scope="scope"  -->
          <template slot-scope="scope">
            <el-tag :type="articleStates[scope.row.status].type">{{ articleStates[scope.row.status].text }}</el-tag>
            <!-- <el-tag v-if="scope.row.status === 0">草稿</el-tag>
            <el-tag v-else-if="scope.row.status === 1" type="warning">待审核</el-tag>
            <el-tag v-else-if="scope.row.status === 2" type="success">审核通过</el-tag>
            <el-tag v-else-if="scope.row.status === 3" type="danger">审核失败</el-tag>
            <el-tag v-else-if="scope.row.status === 4" type="primary">已删除</el-tag> -->
          </template>
        </el-table-column>
        <el-table-column
          prop="pubdate"
          label="发布时间"
          align="center"
        >
        </el-table-column>
        <el-table-column
          prop="address"
          label="操作"
          align="center"
        >
          <template slot-scope="scope">
            <el-button
              size="small"
              type="primary"
              icon="el-icon-edit"
              circle
              @click="$router.push('/publish?id=' + scope.row.id.toString())"
            ></el-button>
            <el-button
              size="small"
              type="danger"
              icon="el-icon-delete"
              circle
              @click="onDeleteArticle(scope.row.id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- /数据列表 -->
      <!-- 分页器 -->
      <!-- total 用来设定总数据的条数。   它默认安装 10 条每页计算总页码
      page-size 是每页显示多少条数据，默认是 10 条，可以自行控制
      获取当前点击的页码数：current-page.sync，sync必须加上，否则不能动态获取到当前点击的页码数-->
      <el-pagination
        class="pagination"
        background
        layout="prev, pager, next"
        :total="totalCount"
        :disabled="loading"
        :page-size="pageSize"
        :current-page.sync="page"
        @current-change="onCurrentChange"
      />
      <!-- /分页器 -->
    </el-card>
  </div>
</template>

<script>
import {
  getArticles,
  getArticleChannels,
  deleteArticle
} from '@/api/article'
export default {
  name: 'ArticleIndex',
  components: {},
  props: {},
  data () {
    return {
      form: {
        name: '',
        region: '',
        date1: '',
        date2: '',
        delivery: false,
        type: [],
        resource: '',
        desc: ''
      },
      articles: [], // 文章数据列表
      articleStates: [
        { status: 0, type: '', text: '草稿' },
        { status: 1, type: 'warning', text: '待审核' },
        { status: 2, type: 'success', text: '审核通过' },
        { status: 3, type: 'danger', text: '审核失败' },
        { status: 4, type: 'primary', text: '已删除' },
        { status: 5, type: 'info', text: '无内容' }
      ],
      totalCount: 1, // 总共数据条数
      pageSize: 20, // 每页数据的大小
      status: null, // 查询文章的状态，不传就是全部，也就是null是全部
      channels: [], // 文章频道列表
      channelId: null, // 查询文章的频道
      rangeDate: null, // 筛选的范围日期
      loading: true, // 加载状态
      page: 1 // 动态获取当前被点击的页码
    }
  },
  computed: {},
  watch: {},
  created () {
    this.loadArticles()
    this.loadChannels()
  },
  mounted () {},
  methods: {
    loadArticles (page = 1) {
      this.loading = true
      getArticles({
        // page: 1,
        // page: page,
        page,
        per_page: this.pageSize,
        status: this.status,
        channel_id: this.channelId,
        begin_pubdate: this.rangeDate ? this.rangeDate[0] : null, // 起始时间
        end_pubdate: this.rangeDate ? this.rangeDate[1] : null // 截止时间
      }).then(res => {
        // 在数据结构的时候，不允许使用类似 total_count 的带有下滑先的名字，所以我们需要自己命名为 totalCount
        const { results, total_count: totalCount } = res.data.data
        this.articles = results
        this.totalCount = totalCount
        // this.articles = res.data.data.results
        // this.totalCount = res.data.data.total_count

        // 当数据回来之后，关闭loading状态
        this.loading = false
      })
    },

    onCurrentChange (page) {
      this.loadArticles(page)
    },

    loadChannels () {
      getArticleChannels().then(res => {
        const { channels } = res.data.data
        this.channels = channels
      })
    },

    // 删除文章
    onDeleteArticle (articleId) {
      // 点击删除按钮时，弹出提示框
      this.$confirm('确认删除吗?', '删除提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 删除时的提示弹框消息
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
        deleteArticle(articleId.toString()).then(res => {
          // 删除成功，更新当前页的文章数据列表
          this.loadArticles(this.page)
        })
      }).catch(() => {
        this.$message({
          type: 'success',
          message: '取消删除'
        })
      })
    }
  }
}
</script>

<style scoped lang="less">
  .filter-card {
    margin-bottom: 30px;
  }
  .list-table {
    margin-bottom: 30px;
    .article-cover {
      width: 100px;
      max-height: 200px;
      overflow: hidden;
      img {
        width: 100%;
      }
    }
  }
  .pagination {
    text-align: center;
  }
</style>
